Many of the different PIC microcontrollers contain an area of EEPROM memory. EEPROM stands for Electrically Erasable Programmable Read Only Memory, which translates to "memory which holds its value when the power is turned off". EEPROM is not like the PIC microcontroller's register memory, in that you can't just put values into it, instead you have to instruct the PIC microcontroller to write or read a particular location.
EEPROM is useful as a way of replacing hardware like switches or lines which would otherwise have to be used to configure a device. Because it is important that such information is not corrupted by rogue programs the designers of the PIC microcontroller have devised a sequence of actions you need to perform in order to store values in EEPROM. This sequence is given in detail in Practical 7, the digital lock, where you will also find code to read and write to this memory.
Writing to EEPROM
To write to the EEPROM you must follow this sequence:
-
set the address of the destination in EEADR bank 0
-
put the data in EEDATA bank 0
-
turn off all the interrupts (clear GIE in INTCON) bank 0
-
set the write enable bit (WREN) in EECON1 bank 1
-
put the value 0x55 in EECON2 bank 1
-
put the value 0xAA in EECON2 bank 1
-
set the write bit (WR) in EECON1 bank 1
-
wait for the EEIF bit in EECON1 to set bank 1
-
clear the EEIF bit in EECON1 bank 1
-
clear the WREN bit in EECON1 bank 1
-
turn interrupts back on (set GIE in INTCON) bank 1
This rather long winded process will write a single byte to EEPROM. Note that EECON1 and EECON2 are in bank 1 and so your code must set the bit in the status register for this to work correctly.
Reading from EEPROM
To read from the EEPROM is much easier. You just need to follow this sequence:
-
set the address of the source in EEADR bank 0
-
set RD bit in EECON1 bank 1
-
read the data from EEDATA bank0.
I have put these actions into the functions which I have written. See the code in Ex71 for this.
EEDATA - EE Data Register
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
- |
- |
- |
- |
- |
- |
- |
- |
This register is used to hold data being sent to, or read from the EEPROM. If you are writing to the EEPROM you put the value to be written into this register. If you are reading the PICmicro will put the value it has read into this register for you to collect.
EEADR - EE Address Register
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
- |
- |
- |
- |
- |
- |
- |
- |
You use this register to tell the PIC microcontroller which location in the EEPROM you wish to interact with. The register is 8 bits wide, which implies 256 locations. However, in the PIC microcontroller we are using only the bottom 32 locations are available.
EECON1 - EE Control Register 1
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
- |
- |
- |
EEIF |
WREER |
WREN |
WR |
RD |
Bits 7, 6 and 5 are not used in this register.
EEIF - EEPROM write operation Interrupt Flag
If this flag is set it indicates that a write operation on the EEPROM has completed. If you have enabled interrupts from the EEPROM write operation you will need to clear this bit in your interrupt handler to allow another interrupt to be generated from the next write. If you are not using interrupts you can still look at this bit to see if an EEPROM write has completed.
WREER - WRite Eeprom ERror
This bit is set if a write failed. A write could fail if the PIC microcontroller is reset or a watchdog timeout occurs during the write operation. In either of these cases this bit is set to indicate that the write did not succeed. You should check this bit after a write to ensure that it completed OK.
WREN - WRite ENable
This bit must be set to enable a write operation. You must set this bit at the start of the write sequence. You should clear the bit at the end of your writes, to prevent inadvertent writes to EEPROM. The PIC microcontroller automatically clears this bit on reset and also each time the watchdog timer times out (if it is enabled).
WR - WRite
You set this bit when you want to write to the EEPROM. You can't clear the bit, it is cleared by the hardware when the write is complete. Note that because writing to EEPROM is rather slow, this bit is not cleared immediately after it being set.
RD - ReaD
You set this bit when you want to read the EEPROM memory. As with the write bit, you do not clear the bit, it is cleared for you by the PICmicro when the read has been performed. Note that unlike the write operation, read operations complete in time for the next instruction.
EECON2 - EE Control Register 2
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
- |
- |
- |
- |
- |
- |
- |
- |
Individual bits in this register are not used, instead it is used as part of the write protection process.